package com.duyutian.controller.hiwodytController;

import com.duyutian.common.ServiceResult;
import com.duyutian.controller.hiwodytController.service.LogService;
import com.duyutian.model.Article;
import com.duyutian.model.ArticleCategory;
import com.duyutian.model.ArticleCategoryMapping;
import com.duyutian.model.User;
import com.duyutian.util.NumberUtils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dyt on 2018/9/29.
 */
/*跳转表现层*/
@Controller
public class IndexController {
    @Autowired
    private LogService logService ;

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {

        //加载首页数据
        List<Article> articles = new Article().dao().find("select * from article order by created desc");
        model.addAttribute("articles", articles);


        //大广告
        //一级分类
        List<ArticleCategory> articleCategories = ArticleCategory.dao.find("SELECT * FROM `article_category` where pid = 0 order by idx asc");

        model.addAttribute("cats",articleCategories);

        return "hiwodyt/index";
    }
        /*文章跳转*/
    @RequestMapping("/hiwodyt/article/{id}")
    public String article(@PathVariable("id")Integer id ,Model model){
        //文章详情
        Article article = Article.dao.findById(id);
        //文章访客加1
        article.setViewCount(article.getViewCount()+1l);
        article.update();
        //文章分类
        ArticleCategoryMapping byId = ArticleCategoryMapping.dao.findById(NumberUtils.parseInt(article.getId(),0));
        ArticleCategory twoCat = ArticleCategory.dao.findById(NumberUtils.parseInt(byId.getCategoryId(),0));
        ArticleCategory firstCat = ArticleCategory.dao.findById(NumberUtils.parseInt(twoCat.getPid(),0));
        //文章评论
        List<Record> records = Db.find("SELECT * FROM article_comment where is_delete = 0 and article_id = ? ",article.getId());
        ArrayList<Record> comments  = new ArrayList<Record>();
        for (Record obj:records ) {
            if(obj.getInt("parent_id")!=0){
                for (Record r:comments){
                    if(r.getInt("id")==obj.getInt("parent_id")){
                        r.set("innerComment",obj);
                    }
                }
            }else{
                obj.set("innerComment",null);
                comments.add(obj);
            }
        }
        User user = User.dao.findById(article.getUserId());
        model.addAttribute("user",user);
        model.addAttribute("comments", ServiceResult.rsFormat1(comments));
        model.addAttribute("firstCat",firstCat.getTitle());
        model.addAttribute("twoCat",twoCat.getTitle());
        model.addAttribute("article", article);
        model.addAttribute("commentNum",comments.size());
        return "hiwodyt/article";
    }
    @RequestMapping("/hiwodyt/about")
    public String about(){
        return "hiwodyt/about";
    }


}
